﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TouristAgencyDataLayer;

namespace TouristAgencyWinApp
{
    public partial class DestinationsForm : Form
    {
        private Destination selectedDest;

        public DestinationsForm()
        {
            InitializeComponent();
            WinLoad();
        }

        private void WinLoad()
        {
            destinationBindingSource.DataSource = Destinations.GetDestinations();
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            if (selectedDest != null && !string.IsNullOrEmpty(this.hfTxt.Text))
            {
                Destinations.UpdateDestination(selectedDest.DestinationId, this.hfTxt.Text);
                MessageBox.Show("Changes have been saved");
            }
            else if (!string.IsNullOrEmpty(this.hfTxt.Text))
            {
                Destinations.CreateDestination(this.hfTxt.Text);
                MessageBox.Show("New Destination has been added");
            }
            WinLoad();
        }

        private void hotelFDG_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != this.destDG.Rows.Count - 1)
            {
                selectedDest = Destinations.GetDestinationById(((Destination)this.destDG.Rows[e.RowIndex].DataBoundItem).DestinationId);
                this.hfTxt.Text = selectedDest.Destination1;
            }
            else
            {
                this.hfTxt.Text = "";
                selectedDest = null;
            }
        }

        private void delBtn_Click(object sender, EventArgs e)
        {
            if (selectedDest != null)
            {
                if (!Destinations.TourWithThatDestination(selectedDest.DestinationId))
                {
                    Destinations.DeleteDestination(selectedDest.DestinationId);
                    MessageBox.Show("The selected item has been deleted");
                }
                else
                {
                    MessageBox.Show("You can not delete that destination, there is a tour with that destination");
                }
            }
            else
            {
                MessageBox.Show("Select some item first.");
            }
            WinLoad();
        }
    }
}
